<template>
  <div id="login-container">
    <div class="login-box">
      <div class="avatar-box">
        <img src="../assets/logo.png">
      </div>
      <!--  form 表单区域 start      -->
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" class="login_form">
        <!--        //用户名-->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon=" el-icon-user-solid
          "></el-input>
        </el-form-item>
        <!--        //密码-->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" prefix-icon=" el-icon-s-goods
          "></el-input>
        </el-form-item>
        <el-form-item class="yes_or_btn">
          <el-button type="primary" @click="logining">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
      <!--  form 表单区域 end-->
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '121'
      },
      loginFormRules: {
        username: [
          {
            require: true,
            message: '请输入用户名',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 10,
            message: '长度在3-10之间',
            trigger: 'blur'
          }
        ],
        password: [
          {
            require: true,
            message: '请输入密码',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    logining () {
      this.$refs.loginFormRef.validate(valid => {
        if (!valid) return
        const  result=this.$http.post('/login',this.loginForm);
        console.log("请求服务器 获取访问数据： "+result);
        if (this.loginForm.username == '123') {
          this.$message.success('登录成功')
          window.sessionStorage.setItem('token', '123121343')
          //数据库验证登录成功  将返回token存入sessionStorage中
          //项目中除了登录 其它所有的api接口访问需携带token
          //
          this.$router.push("/index");
        } else {
          this.$message.error('登录失败')
        }
      })
    },
    resetLoginForm () {
      console.log(this)
      this.$refs.loginFormRef.resetFields()
    }
  }
}
</script>

<style scoped>
#login-container {
  background-color: #2b4b6b;
}

.login-box {
  width: 450px;
  height: 300px;
  border-radius: 3px;
  background-color: white;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.avatar-box {
  width: 120px;
  height: 120px;
  padding: 10px;
  border: 1px solid #eee;
  border-radius: 50%;
  box-shadow: 0 0 10px #ddd;
  background-color: #fff;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
}

.avatar-box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.yes_or_btn {
  display: flex;
  justify-content: flex-end;
}
</style>
